﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Input;
using BBInterfaceNET.ProjectExplorer.Model;
using BBInterfaceNET.ProjectExplorer.ViewModels;

namespace BBInterfaceNET.ProjectExplorer.Behaviors
{
    public class DoubleClickBehavior : Behavior<TreeView>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.MouseDoubleClick += AssociatedObject_MouseDoubleClick;
        }

        void AssociatedObject_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            FileNode res = AssociatedObject.SelectedItem as FileNode;
            if (res == null) return;
            ExplorerViewModel vm = AssociatedObject.DataContext as ExplorerViewModel;            
            if (vm == null) return;

            vm.OpenFileCommand.Execute(res);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.MouseDoubleClick -= AssociatedObject_MouseDoubleClick;
        }
    }
}
